package com.pioneer.swing.controller;

import com.pioneer.swing.ui.*;
import com.pioneer.swing.model.AllContainersTable;
import com.pioneer.swing.model.AllManifestsTable;
import com.pioneer.swing.model.ContainerEntryTable;
import com.pioneer.beans.*;
import com.pioneer.dao.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JInternalFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.InternalFrameEvent;
import javax.swing.JOptionPane;

/**
 * Title:        Pioneer Metals
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class RowRemover implements ActionListener {

  private JInternalFrame frame;
  private JTable table;
  private ManifestDAO manifestDAO;
	private JobDAO jobDAO;
	private ContactDAO contactDAO;
	
	public void setManifestDAO(ManifestDAO manifestDAO) {
		this.manifestDAO=manifestDAO;
	}
	
	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}
	
	public void setJobDAO(JobDAO jobDAO) {
		this.jobDAO = jobDAO;
	}

  public RowRemover(JTable table) {
	this(table, null);
  }

  public RowRemover(JTable table, JInternalFrame frame) {
	this.table=table;	
	this.frame=frame;
  }

  public static void main(String[] args) {
  }

  public void actionPerformed(ActionEvent e) {
    String actionCommand=e.getActionCommand();
    Object queryObj=e.getSource();
	System.out.println("The action command is "+actionCommand);
	if (actionCommand.equals(UIConstants.COMMAND_DELETE_MANIFEST)) {
		Key key=((Keyed)queryObj).getKey();
		ManifestKey manifestKey=(ManifestKey)key;
		String jobNum=manifestKey.getJobNum();
		int returnInt = JOptionPane.showConfirmDialog(frame,
			UIConstants.MESG_DELETE_CONFIRM,
			UIConstants.TITLE_DELETE_CONFIRM,
			JOptionPane.YES_NO_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				System.out.println("Deleting manifest");
				try {
					manifestDAO.deleteManifest(jobNum);
					ManifestsFrame manifestsFrame = (ManifestsFrame)this.frame;
					manifestsFrame.fireInternalFrameEvent();
				}
				catch (DataAccessException ex) {
					System.err.println("Cannot delete the row because "+ex.getMessage());
				}
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
		}
	}
	else if (actionCommand.equals(UIConstants.COMMAND_DELETE_JOB)) {
		Key key=((Keyed)queryObj).getKey();
		JobKey jobKey=(JobKey)key;
		String[] quoteNums=jobKey.getQuoteNums();			
		int returnInt = JOptionPane.showConfirmDialog(frame,
		UIConstants.MESG_DELETE_CONFIRM,
		UIConstants.TITLE_DELETE_CONFIRM,
		JOptionPane.YES_NO_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				System.out.println("Deleting job quotes");
				try {
					if ((quoteNums!=null) && (quoteNums.length>0)) {
						for (int count=0;count<quoteNums.length;count++) {
							jobDAO.deleteJob(quoteNums[count]);					
						}
						((JobsFrame)this.frame).fireInternalFrameEvent();
					}
				}
				catch (DataAccessException ex) {
					System.err.println("Cannot delete the row because "+ex.getMessage());
				}
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
		}
		
	}
	else if (actionCommand.equals(UIConstants.COMMAND_DELETE_CONTAINER)) {
		Key key=((Keyed)queryObj).getKey();
		ContainerKey containerKey=(ContainerKey)key;
		String jobNum=containerKey.getJobNum();
		String containerId=containerKey.getId();
		int returnInt = JOptionPane.showConfirmDialog(frame,
			UIConstants.MESG_DELETE_CONFIRM,
			UIConstants.TITLE_DELETE_CONFIRM,
			JOptionPane.YES_NO_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				//System.out.println("Deleting container");
				try {
					manifestDAO.deleteContainer(jobNum,containerId);
				}
				catch (DataAccessException ex) {
					System.err.println("Cannot delete the row because "+ex.getMessage());
				}
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
		}
	}
	else if ((actionCommand.equals(UIConstants.COMMAND_DELETE_CONTAINER_CONTENT)) || (actionCommand.equals(UIConstants.COMMAND_DELETE_ITEM)))
	{
		int selectedRow=table.getSelectedRow();
		System.out.println("The selected row is "+selectedRow);
		if (selectedRow>-1) {			
			((DefaultTableModel)table.getModel()).removeRow(selectedRow);
		}
	}
  }
}